
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  event;

import java.io.*;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.Hashtable;

import logger.PoolLogger;
import utility.*;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class EventHandler extends Thread
{
	String path = Input.PROCESSLOGGERPATH;
	//String masterPath = Input.MASTERREPORTSPATH;
	
	//String fileFormat = ".txt";
	String fileName;
	RandomAccessFile rwFile;

	final String MSG_ID = "msgid";	
	final String REQUEST = "request";
	final String PROCESS = "process";
	final String STATUS = "status";
	final String SCHEDULEDATE="scheduledate";
	final String MASTEROBJECT = "masterobject";
	final String USERNAME = "username";
	final String TYPE="mastertype";
	final String PRODUCT="product";	
	final String DATETIME="datetime";
	
	GetIdName gin;
	final String tablename= "eventprocess";
	ResultSet resultset;
	PoolLogger pl;
	
	public EventHandler() 
	{	
		gin = new GetIdName();
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	public void run()
	{
		gin = new GetIdName();
		
		while(true)
		{	
			try 
			{
				initialize();
				sleep(2000);
			} catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
	}
	public void initialize()
	{
		try
		{
			Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
			Condition.put("status", 0);			
			resultset = DBManager.getselect(Condition, tablename);
			this.sleep(1000);
			getEvent();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unchecked")
	void getEvent() 
	{
		try 
		{
			String line ="";
			while(resultset.next())
			{
				try
				{	
					System.out.println("event::EventHandler::getEvent:: The Line is "+line);
					pl.info("event::EventHandler::getEvent:: The Line is "+line);
					pl.debug("event::EventHandler::getEvent:: The Line is "+line);
					pl.warn("event::EventHandler::getEvent:: The Line is "+line);
					pl.fatal("event::EventHandler::getEvent:: The Line is "+line);
					pl.error("event::EventHandler::getEvent:: The Line is "+line);
					pl.trace("event::EventHandler::getEvent:: The Line is "+line);
					
					String Trans_type = resultset.getString(TYPE);
					String event_id = resultset.getString(MASTEROBJECT);
					String datetime = resultset.getString(DATETIME);		
					System.out.println("event::EventHandler::getEvent:: The Line is "+line);
					Hashtable<Object,Object> temp = new Hashtable<Object, Object>();
					RecordSearch rs = new RecordSearch();
					temp.put("td", "null");
					temp.put("mid", event_id);			
					//String request[] = rs.getArrayFromResultSet();
					String Process=resultset.getString(PROCESS);
					//String process[] = rs.getArrayFromResultSet();
				//	if(process.length>0 && request.length>0)
					{
							temp.put("pid",gin.getId("type"));
							int pv_index = Column.pv_index-1;
							int vt_index = Column.vt_index-1;
							rs.setConditionMap(temp);
							String [] getEvent = rs.getArrayFromResultSet()[0].split("\t");
							String eventValue = getEvent[pv_index];
							if(eventValue.equals("0"))
								eventValue = getEvent[vt_index];
							else
								eventValue = gin.getItem(eventValue);						
							Event startEvent;
							System.out.println("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.info("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.debug("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.warn("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.fatal("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.error("event::EventHandler::getEvent:: Start Event "+eventValue);
							pl.trace("event::EventHandler::getEvent:: Start Event "+eventValue);
							
							String msgid=resultset.getString(MSG_ID);
							String request=resultset.getString(REQUEST);
							Class C = Class.forName("event."+CapitalChar.makeFirstCharCapital(eventValue));
							startEvent = (Event)C.newInstance();
							startEvent.SetData(resultset.getString(PRODUCT), Trans_type,msgid, resultset.getString(USERNAME),Process,datetime,request);
							startEvent.initializationData();
							startEvent.setObject(event_id);							
							String Methodname = "after"+CapitalChar.makeFirstCharCapital(Process);
							Method method = C.getDeclaredMethod(Methodname,new Class[0]);
							Boolean result = (Boolean) method.invoke(startEvent, new Object[0]);							
							if(result.booleanValue()==true)
							{	
								DBManager.getUpdate("update eventprocess set status=5 where msgid='"+msgid+"' and masterobject='"+event_id+"'");
							}	
					}		
				}
				catch (Exception e) 
				{
					e.printStackTrace();
					System.out.println(e.getMessage());
					pl.info(e.getMessage());
					pl.debug(e.getMessage());
					pl.warn(e.getMessage());
					pl.fatal(e.getMessage());
					pl.error(e.getMessage());
					pl.trace(e.getMessage());
					
				}
					}
				}
				catch (Exception e) 
				{
				}
	}
	
	void startEvent(String Event)
	{
		System.out.println("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.info("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.debug("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.warn("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.fatal("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.error("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
		pl.trace("event::EventHandler::startEvent:: The "+Event+" Event is Starts Here ");
	}
	public static void main(String []a)
	{
		EventHandler eHandler = new EventHandler();
		eHandler.initialize();
		
	}
}
